//
//  MedsViewController.m
//  iHealthEHR
//
//  Created by Joseph Ayo Akinyele on 3/22/10.
//  Copyright 2010 __MyCompanyName__. All rights reserved.
//

#import "MedsViewController.h"


@implementation MedsViewController
@synthesize webView, curCategory;
@synthesize activityIndicator;

// The designated initializer.  Override if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
}

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
	[self.navigationController setToolbarHidden:YES];	
	// [self loadMySection];
	[super viewDidLoad];
}

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated {
	[self.navigationController setToolbarHidden:YES];	
	[webView reload];
#ifdef HEALTH_DEBUG
	NSLog(@"In viewWillAppear => %@", self.curCategory);
#endif
	[self loadMySection];
    [super viewWillAppear:animated];
}

- (void)dealloc {
	[webView release];
	[curCategory release];
	[activityIndicator release];
    [super dealloc];
}

#pragma mark -
#pragma mark Methods to iPhone filesystem

-(void) writeToWeb:(NSXMLElement *) element
{
#ifdef HEALTH_DEBUG	
	NSLog(@"Overwriting file in tmp directory:");
	NSLog(@"%@", element);
#endif
	NSXMLDocument *docRoot = [[NSXMLDocument alloc] initWithRootElement: element];
	
	// destination file is written to
	NSString *outfile = [@"~/tmp/data.xml" stringByExpandingTildeInPath];
	
	// ensure same file is overwritten every time we launch dashcode app.
	[[docRoot XMLData] writeToFile:outfile atomically:YES];
	
	[docRoot release];
}

-(void) loadMySection
{
	NSString *link = [[NSString alloc] initWithFormat:@"index-%@", self.curCategory]; 
	NSString *path = [[NSBundle mainBundle] pathForResource:link 
													 ofType:@"html"];
#ifdef HEALTH_DEBUG	
	NSURL *url = [NSURL fileURLWithPath:path isDirectory:NO];
	NSLog(@"Loading URL: %@", [url absoluteURL]);
#endif
	[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath: path 
																 isDirectory:NO] ]];
	[link release];
}

#pragma mark -
#pragma mark UIWebView delegate methods
-(void) webViewDidStartLoad: (UIWebView *) webView {
	[activityIndicator startAnimating];
}

-(void) webViewDidFinishLoad:(UIWebView *)webView {
	[activityIndicator stopAnimating];	
}

@end
